package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.NetUtils;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.util.UToast;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.DailNumberAdapter;
import com.umeox.watch.moto.launcher.ui.help.GalleryLayoutManager;
import com.umeox.watch.moto.launcher.ui.help.ScaleTransformer;

/**
 * Created by Rambo 2023/7/4
 * Describe:
 */
public class DialPadActivity1 extends ThemeAppCompatActivity implements View.OnClickListener, View.OnLongClickListener, DailNumberAdapter.OnItemClickListener {


    private String[] mItemTexts = new String[]{"1", "2", "3",
            "4", "5", "6", "7", "8", "9", "0", "*", "#", "+", ","};
    private ImageView mDeleteIv;
    private EditText mPhoneNumberTv;
    private ImageView mDialIv;

    private DailNumberAdapter mDailNumberAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dial_pad1);
        mDeleteIv = findViewById(R.id.iv_delete);
        mPhoneNumberTv = findViewById(R.id.tv_phone_number);
        mDialIv = findViewById(R.id.iv_dial);

        mDailNumberAdapter = new DailNumberAdapter(this, mItemTexts);
        RecyclerView dailRv = findViewById(R.id.rv);
        dailRv.setHasFixedSize(true);
        dailRv.setAdapter(mDailNumberAdapter);
        mDailNumberAdapter.setOnItemClickListener(this);


//        dailRv.setLayoutManager(new EnlargeLinearLayoutManager(this));


        GalleryLayoutManager galleryLayoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        galleryLayoutManager.setItemTransformer(new ScaleTransformer());
        galleryLayoutManager.attach(dailRv);


        mDeleteIv.setOnClickListener(this);
        mDeleteIv.setOnLongClickListener(this);
        mDialIv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_delete) {
            if (!TextUtils.isEmpty(mPhoneNumberTv.getText().toString().trim())) {
                mPhoneNumberTv.setText(mPhoneNumberTv.getText().subSequence(0, mPhoneNumberTv.getText().length() - 1));
            }
        } else if (v.getId() == R.id.iv_dial) {

            String number = mPhoneNumberTv.getText().toString().trim();

            if (!TextUtils.isEmpty(number)) {
                if (PhoneNumberUtils.isEmergencyNumber(number)) {
                    Intent intent = new Intent("android.intent.action.CALL_PRIVILEGED", Uri.parse("tel:" + number));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } else {
                    if (!NetUtils.hasSimCard()) {
                        UToast.showCustomToast(DialPadActivity1.this, getString(R.string.no_simcard_str), Toast.LENGTH_LONG);
                        return;
                    }
                    Intent callIntent = new Intent();
                    callIntent.setAction(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + number));
                    startActivity(callIntent);
                }

            }
        }

    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.iv_delete) {
            if (!TextUtils.isEmpty(mPhoneNumberTv.getText())) {
                mPhoneNumberTv.setText("");
            }
            return true;

        }
        return false;
    }

    @Override
    public void onItemClick(String value) {
        Logger.e("text=" + value);
        mPhoneNumberTv.setText(String.format("%s%s", mPhoneNumberTv.getText(), value));
        mPhoneNumberTv.setSelection(mPhoneNumberTv.getText().length());
    }
}
